<template>
    <div class="system-offer-container layout-padding">
        <el-card shadow="hover" class="layout-padding-auto">
            <el-card shadow="hover" class="layout-padding-auto">

                <div class="bar_tools">
                    <el-button-group class="ml-4">
                        <el-button type="primary" size="default" @click="addItem()">添加分类</el-button>
                    </el-button-group>
                </div>

                <el-table size="default" :data="state.dataList" style="width: 100%">
                    <el-table-column width="80" label="ID">
                        <template #default="scope">
                            <span>{{ scope.row.id }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="分类名称">
                        <template #default="scope">
                            <span>{{ scope.row.title }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="分类编码">
                        <template #default="scope">
                            <span>{{ scope.row.classCode }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="所属语言">
                        <template #default="scope">
                            <span v-if="scope.row.languageType == 2">英文</span>
                            <span v-if="scope.row.languageType == 3">中文繁体</span>
                            <span v-if="scope.row.languageType == 4">越南语</span>
                            <span v-if="scope.row.languageType == 5">日语</span>
                            <span v-if="scope.row.languageType == 6">韩语</span>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" label="排序">
                        <template #default="scope">
                            <span>{{ scope.row.articleSort }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column width="200" align="center" label="创建日期">
                        <template #default="scope">
                            <span>{{ formatDate2(scope.row.createTime) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="创建人">
                        <template #default="scope">
                            <span>{{ scope.row.createBy }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="更新人">
                        <template #default="scope">
                            <span>{{ scope.row.updateBy }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="状态">
                        <template #default="scope">
                            <el-tag v-if="scope.row.status == 1" effect="dark" type="success">启用</el-tag>
                            <el-tag v-if="scope.row.status != 1" effect="dark" type="danger">禁用</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="操作">
                        <template #default="scope">
                            <div>
                                <el-button size="small" type="primary"
                                    @click.native.prevent="editItem(scope.row)">编辑</el-button>
                            </div>
                            <div v-if="scope.row.status == 1" style="margin-top:10px;">
                                <el-button size="small" type="danger"
                                    @click.native.prevent="changeItemState(scope.row)">禁用</el-button>
                            </div>
                            <div v-if="scope.row.status != 1" style="margin-top:10px;">
                                <el-button size="small" type="success"
                                    @click.native.prevent="changeItemState(scope.row)">启用</el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </el-card>

            <el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
                :pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.page" background
                v-model:page-size="state.limit" layout="total, sizes, prev, pager, next, jumper" :total="state.total">
            </el-pagination>
        </el-card>

        <actegoryDialog ref="actegoryDialogRef" @refresh="refresh"></actegoryDialog>
    </div>
</template>

<script setup lang="ts" name="systemMenu">

import { formatDate } from '/@/utils/formatTime';
import { onMounted, reactive, defineAsyncComponent, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';


import { useCommonApi } from '/@/api/common/index'


// 引入组件
const actegoryDialog = defineAsyncComponent(() => import('./dialog.vue'));
const actegoryDialogRef = ref();

const state = reactive({
    page: 1,
    limit: 10,
    total: 0,
    dataList: [] as any,
    ruleForm: {}
});







const refresh = () => {
    articleCategory();
}


const editItem = (row: any) => {
    actegoryDialogRef.value.openDialog('edit', row);
}


const addItem = () => {
    actegoryDialogRef.value.openDialog('add');
}

const formatDate2 = (str: any) => {
    if (!str) return "-";
    return formatDate(new Date(str), "YYYY-mm-dd HH:MM")
}


// 分页改变
const onHandleSizeChange = (val: number) => {
    state.limit = val;
    articleCategory();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
    state.page = val;
    articleCategory();
};



const articleCategory = async () => {
    let vo = {
        page: state.page,
        limit: state.limit,
        nodeType: 1
    } as any;
    let tempList = await useCommonApi().articleList(vo);
    state.dataList = tempList.list;
    state.total = tempList.total;
}


// 改变文章分类的状态
const changeItemState = (item: any) => {
    let statuTemp = item.status == 1 ? 0 : 1;
    let txtMsg = statuTemp == 1 ? "您是否要启用此分类?" : "您是否要禁用此分类?";
    ElMessageBox.confirm(txtMsg, '提示', {
        type: "warning",
        confirmButtonText: '确认',
        cancelButtonText: "取消"
    }).then(async () => {
        try {
            let obj = {
                status: statuTemp
            };
            obj = Object.assign({}, item, obj);
            await useCommonApi().updateArticle(obj);
            ElMessage.success("操作成功")
            item.status = statuTemp;
        } catch (error: any) {
            ElMessage.error(error.msg);
        }
    });
}




// 页面加载时
onMounted(() => {
    articleCategory();
});



</script>




<style scoped lang="scss">
.bar_tools {
    display: flex;
    flex-direction: row-reverse;
    margin-bottom: 10px;
}

.system-offer-container {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}
</style>

